<template>
  <view class="container">
    <!-- 食堂信息 -->
    <view class="canteen-info-section" v-if="canteen">
      <image class="canteen-bg" :src="canteen.image" mode="aspectFill"></image>
      <view class="canteen-overlay"></view>
      <view class="canteen-info">
        <view class="canteen-name">{{ canteen.name }}</view>
        <view class="canteen-floor">{{ canteen.floor }}</view>
        <view class="canteen-merchant-count">{{ canteen.merchantCount }}家商户</view>
        <view class="canteen-desc">{{ canteen.description }}</view>
      </view>
    </view>

    <!-- 商户列表 -->
    <view class="merchant-section">
      <view class="section-header">
        <text class="section-title">商户列表</text>
      </view>

      <view class="merchant-list">
        <view class="merchant-card" v-for="(item, index) in merchantList" :key="index"
          @click="goToMerchantDetail(item.id)">
          <image class="merchant-logo" :src="item.logo" mode="aspectFill"></image>
          <view class="merchant-info">
            <view class="merchant-header">
              <text class="merchant-name">{{ item.name }}</text>
              <!-- 评分暂时隐藏 -->
        <!-- <view class="rating-box">
          <text class="rating-score">{{ item.rating }}</text>
          <text class="rating-star">★</text>
        </view> -->
            </view>
            <view class="merchant-meta">
              <text class="meta-item">{{ estimateTime(item.baseTime, item.distance) }}</text>
              <text class="meta-divider">|</text>
              <text class="meta-item">{{ item.distance }}km</text>
              <text class="meta-divider">|</text>
              <text class="meta-item">起送¥{{ item.minPrice }}</text>
            </view>
            <view class="merchant-tags">
              <view v-if="item.deliveryFee === 0" class="tag delivery-free">免配送费</view>
              <view v-else class="tag delivery-fee">配送费¥{{ item.deliveryFee }}</view>
              <view v-if="item.sales > 500" class="tag sales-tag">月售{{ item.sales }}+</view>
            </view>
          </view>
        </view>

        <view v-if="merchantList.length === 0" class="empty-tip">
          <image src="/static/icons/empty.png" mode="aspectFit" class="empty-icon"></image>
          <text class="empty-text">该食堂暂无商户</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { getCanteenById } from '@/api/canteen.js';
import { getMerchantsByCanteenId } from '@/api/merchant.js';
import estimateTime from '@/utils/time.js';
import { getCurrentLocation } from '@/utils/location.js';

// 获取食堂ID
const canteenId = ref(null);
const canteen = ref(null);
const merchantList = ref([]);

// 根据ID获取食堂信息
onLoad(async (options) => {
  console.log("onLoad options===", options);

  if (options.id) {
    canteenId.value = parseInt(options.id);
    
    try {
      // 获取食堂信息
      const canteenRes = await getCanteenById(canteenId.value);
      canteen.value = canteenRes.data;
      
      // 获取该食堂下的商户列表
      const params = {
        canteenId: canteenId.value,
        pageNo: 1,
        pageSize: 1000 // 获取所有商户
    };
      const location = await getCurrentLocation();
      params.longitude = location.longitude;
      params.latitude = location.latitude;
    
      const merchantRes = await getMerchantsByCanteenId(params);
      merchantList.value = merchantRes.data.list || [];
      
    } catch (error) {
      console.error('获取数据失败:', error);
      uni.showToast({
        title: '获取数据失败',
        icon: 'error'
      });
    }
  }
});

// 跳转到商户详情页
const goToMerchantDetail = (merchantId) => {
  uni.navigateTo({
    url: `/pages/merchant/detail?id=${merchantId}`
  });
};
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: #f0f2f5;
}

/* 食堂信息区域 */
.canteen-info-section {
  position: relative;
  height: 400rpx;
  overflow: hidden;
}

.canteen-bg {
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: brightness(0.9);
}

.canteen-overlay {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.6));
}

.canteen-info {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 30rpx;
  color: #fff;
}

.canteen-name {
  font-size: 48rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.5);
}

.canteen-floor {
  font-size: 28rpx;
  margin-bottom: 10rpx;
  opacity: 0.9;
}

.canteen-merchant-count {
  font-size: 26rpx;
  padding: 6rpx 16rpx;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 20rpx;
  display: inline-block;
  margin-bottom: 15rpx;
  backdrop-filter: blur(4px);
}

.canteen-desc {
  font-size: 26rpx;
  opacity: 0.9;
  max-width: 80%;
}

/* 商户列表区域 */
.merchant-section {
  background: #fff;
  border-radius: 30rpx 30rpx 0 0;
  margin-top: -30rpx;
  padding: 30rpx;
  position: relative;
  z-index: 1;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
  position: relative;
  padding-left: 20rpx;
}

.section-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 6rpx;
  height: 28rpx;
  background: linear-gradient(to bottom, #ff9a9e, #ff6b6b);
  border-radius: 6rpx;
}

.merchant-list {
  padding-bottom: 30rpx;
}

.merchant-card {
  display: flex;
  padding: 20rpx;
  margin-bottom: 20rpx;
  background: #ffffff;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
  border: 1rpx solid #f0f0f0;
  transition: all 0.3s;
}

.merchant-card:active {
  transform: scale(0.98);
  background: #fafafa;
}

.merchant-logo {
  width: 120rpx;
  height: 120rpx;
  border-radius: 12rpx;
  margin-right: 20rpx;
  background-color: #f7f7f7;
}

.merchant-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.merchant-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12rpx;
}

.merchant-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.rating-box {
  display: flex;
  align-items: center;
  background: linear-gradient(135deg, #ff9a9e, #ff6b6b);
  padding: 4rpx 12rpx;
  border-radius: 20rpx;
}

.rating-score {
  font-size: 24rpx;
  font-weight: bold;
  color: #fff;
}

.rating-star {
  font-size: 24rpx;
  color: #fff;
  margin-left: 4rpx;
}

.merchant-meta {
  display: flex;
  align-items: center;
  margin-bottom: 12rpx;
}

.meta-item {
  font-size: 24rpx;
  color: #666;
}

.meta-divider {
  margin: 0 10rpx;
  color: #ddd;
}

.merchant-tags {
  display: flex;
  flex-wrap: wrap;
}

.tag {
  font-size: 22rpx;
  padding: 4rpx 12rpx;
  border-radius: 20rpx;
  margin-right: 10rpx;
}

.delivery-free {
  background: #e8f6f0;
  color: #07c160;
}

.delivery-fee {
  background: #fff1f0;
  color: #ff6b6b;
  border: 1rpx solid #ffccc7;
}

.sales-tag {
  background: #f5f5f5;
  color: #999;
}

/* 空状态 */
.empty-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60rpx 0;
}

.empty-icon {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}
</style>
